package com.yanwq.sayhi.common.tool;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Locale;

/**
 * 作者：yanweiqiang。
 * 日期：2016/2/2 15:13。
 */
public class DateFormatTool {
    private static SimpleDateFormat mFormat = new SimpleDateFormat("yyyy年MM月dd日 E", Locale.CHINA);
    private static SimpleDateFormat mFormatE = new SimpleDateFormat("E", Locale.CHINA);
    private static SimpleDateFormat mFormatME = new SimpleDateFormat("MM月dd日 E", Locale.CHINA);
    private static SimpleDateFormat mFormatToday = new SimpleDateFormat("今天 HH:mm", Locale.CHINA);
    private static SimpleDateFormat mFormatYesterday = new SimpleDateFormat("昨天 HH:mm",
            Locale.CHINA);

    public static String format(long timeMillis) {
        Calendar calendar = new GregorianCalendar();
        calendar.setTimeInMillis(timeMillis);
        Calendar today = Calendar.getInstance();

        if (calendar.get(Calendar.YEAR) == today.get(Calendar.YEAR) && calendar.get(Calendar.MONTH) == today.get(
                Calendar.MONTH)) {
            int intervalDay = today.get(Calendar.DAY_OF_MONTH) - calendar.get(Calendar.DAY_OF_MONTH);
            switch (intervalDay) {
                case 0:
                    return mFormatToday.format(timeMillis);
                case 1:
                    return mFormatYesterday.format(timeMillis);
            }
        }

        if (calendar.get(Calendar.YEAR) == today.get(Calendar.YEAR) && calendar.get(Calendar.MONTH) == today.get(
                Calendar.MONTH)) {
            return mFormatE.format(timeMillis);
        }

        if (calendar.get(Calendar.YEAR) == today.get(Calendar.YEAR)) {
            return mFormatME.format(timeMillis);
        }

        return mFormat.format(timeMillis);
    }
}
